using System;
using NUnit.Framework;

namespace BehaveSharp.Specs
{
    [TestFixture]
    public class AccountSpecs
    {
        [Test]
        public void Transfer_to_cash_account()
        {

            Account savings = null;
            Account cash = null;

            Story transferStory = new Story("Transfer to cash account");

            transferStory
                .AsA("savings account holder")
                .IWant("to transfer money from my savings account")
                .SoThat("I can get cash easily from an ATM");

            transferStory
                .WithScenario("Savings account is in credit")

                    .Given("my savings account balance is", 100, delegate(int accountBalance) { savings = new Account(accountBalance); })
                        .And("my cash account balance is", 10, delegate(int accountBalance) { cash = new Account(accountBalance); })
                    .When("I transfer to cash account", 20, delegate(int transferAmount) { savings.TransferTo(cash, transferAmount); })
                    .Then("my savings account balance should be", 80, delegate(int expectedBalance) { Assert.AreEqual(expectedBalance, savings.Balance); })
                        .And("my cash account balance should be", 30, delegate(int expectedBalance) { Assert.AreEqual(expectedBalance, cash.Balance); })

                    .Given("my savings account balance is", 400)
                        .And("my cash account balance is", 100)
                    .When("I transfer to cash account", 100)
                    .Then("my savings account balance should be", 300)
                        .And("my cash account balance should be", 200)

                    .Given("my savings account balance is", 500)
                        .And("my cash account balance is", 20)
                    .When("I transfer to cash account", 30)
                    .Then("my savings account balance should be", 470)
                        .And("my cash account balance should be", 50);

            transferStory
                .WithScenario("Savings account is overdrawn")

                    .Given("my savings account balance is", -20)
                        .And("my cash account balance is", 10)
                    .When("I transfer to cash account", 20)
                    .Then("my savings account balance should be", -20)
                        .And("my cash account balance should be", 10);


        }
    }

    public class Account
    {
        private int accountBalance;

        public Account(int accountBalance)
        {
            this.accountBalance = accountBalance;
        }

        public int Balance
        {
            get { return accountBalance; }
            set { accountBalance = value; }
        }

        public void TransferTo(Account account, int amount)
        {
            if (accountBalance > 0)
            {
                account.Balance = account.Balance + amount;
                Balance = Balance - amount;
            }
        }

        public void Withdraw(int amount)
        {
            if (amount > Balance)
                throw new ArgumentException("Amount exceeds balance", "amount");

            Balance -= amount;
        }
    }




}
